package com.nuonuo.config;

import lombok.Getter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

import java.util.Arrays;

/**
 * @author duan
 * @version 1.0
 * @date 2023/10/25 15:44
 */
@Configuration
@Getter
public class PrimaryRedisConfig {

    @Value("${spring.redis.primary.master}")
    private String master;

    @Value("${spring.redis.primary.nodes}")
    private String nodes;

    @Value("${spring.redis.primary.password}")
    private String password;

    @Value("${spring.redis.primary.database}")
    private int database;

    @Value("${spring.redis.primary.minimumIdleSize}")
    private int minimumIdleSize;

    @Value("${spring.redis.primary.pool.max-active}")
    private int maxActive;

    private static final String CONSTANT_CHAR_COMMA = ",";

    private static final String CONSTANT_CHAR_COLON = ":";

    @SuppressWarnings("Duplicates")
    @Primary
    @Bean(name = "primaryRedisConnectionFactory")
    public RedisConnectionFactory primaryRedisConnectionFactory() {
        RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
        Arrays.asList(nodes.split(CONSTANT_CHAR_COMMA)).forEach((node)->{
            String[] hostAndPort = node.split(CONSTANT_CHAR_COLON);
            RedisNode redisNode = new RedisNode(hostAndPort[0],Integer.parseInt(hostAndPort[1]));
            redisNode.setName(master);
            redisSentinelConfiguration.addSentinel(redisNode);
        });
        redisSentinelConfiguration.setMaster(master);
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisSentinelConfiguration);
        jedisConnectionFactory.setDatabase(database);
        jedisConnectionFactory.setPassword(password);
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMinIdle(minimumIdleSize);
        jedisPoolConfig.setMaxTotal(maxActive);
        jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
        jedisConnectionFactory.afterPropertiesSet();
        return jedisConnectionFactory;
    }

    @SuppressWarnings("Duplicates")
    @Bean(name = "primaryRedisTemplate")
    public RedisTemplate primaryRedisTemplate(
            @Qualifier("primaryRedisConnectionFactory") RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new StringRedisSerializer());
        template.setEnableDefaultSerializer(false);
        template.setConnectionFactory(factory);
        return template;
    }

    @SuppressWarnings("rawtypes")
    @Bean(name = "primaryRedis")
    public RedisUtils primaryRedis(@Qualifier("primaryRedisTemplate") RedisTemplate redisTemplate){
        return new RedisUtils(redisTemplate);
    }
}

